//
//  Consts.swift
//  ASW
//
//  Created by zhuxuhong on 2020/11/22.
//

import UIKit
import DeviceKit

public enum Lets {
    public static func hideKeyboard() {
        UIApplication.shared.keyWindow?.endEditing(true)
    }
    public static func callPhone(_ number: String?) {
        guard let n = number else {
            print("Empty phone number!")
            return
        }
        openUrl("tel://" + n)
    }
    public static func openUrl(_ url: String?, completion: BlockT<Bool>? = nil) {
        guard
            let url = url,
            let aURL = URL(string: url),
            app.canOpenURL(aURL)
        else {
            print("无法打开URL: \(url ?? "Empty")")
            completion?(false)
            return
        }
        if #available(iOS 10.0, *) {
            app.open(aURL, options: [:], completionHandler: completion)
        } else {
            app.openURL(aURL)
            completion?(true)
        }
    }
    
    public static let screen: UIScreen = .main
    public static let device: UIDevice = .current
    public static var defaultTabBarH: CGFloat {
        return UITabBar().intrinsicContentSize.height
    }
    public static var defaultNavBarH: CGFloat {
        return UINavigationBar().intrinsicContentSize.height
    }
    public static var defaultTopBarH: CGFloat {
        return statusBarH + defaultNavBarH
    }
    public static var defaultBottomBarH: CGFloat {
        return homeIndicatorH + defaultTabBarH
    }
    public static var hasHomeIndicator: Bool {
        return homeIndicatorH > 0
    }
    public static var statusBarH: CGFloat {
        return safeAreaInsets.top
    }
    
    public static var safeAreaInsets: UIEdgeInsets {
        if #available(iOS 11.0, *) {
            return appKeyWindow?.safeAreaInsets ?? .zero
        }
        return .zero
    }
    
    public static var homeIndicatorH: CGFloat {
        return safeAreaInsets.bottom
    }
    public static var isIpad: Bool {
        return device.userInterfaceIdiom == .pad
    }
    public static var isLandscape: Bool {
        return app.statusBarOrientation.isLandscape
    }
    public static var isPortrait: Bool {
        return app.statusBarOrientation.isPortrait
    }
    public static var screenBounds: CGRect {
        return screen.bounds
    }
    public static var screenWRatio: CGFloat {
        return screenW / 375
    }
    public static var screenHRatio: CGFloat {
        return screenH / 668
    }
    public static var screenW: CGFloat {
        return screenBounds.width
    }
    public static let screenScale = screen.scale
    public static var screenH: CGFloat {
        return screenBounds.height
    }
    public static let app: UIApplication = .shared
    public static var appDelegate: UIApplicationDelegate? {
        return app.delegate
    }
    public static var appKeyWindow: UIWindow? {
        return appDelegate?.window ?? app.keyWindow
    }
    public static var appRootVC: UIViewController? {
        return appKeyWindow?.rootViewController
    }
    public static var appCurrentVC: UIViewController? {
        guard let vc = appKeyWindow?.rootViewController else {
            return nil
        }
        return topVC(of: vc)
    }
    public static func topVC(of rootVC: UIViewController) -> UIViewController {
        if let tab = rootVC as? UITabBarController,
           let vc = tab.selectedViewController  {
            return topVC(of: vc)
        }
        else if let nav = rootVC as? UINavigationController,
                let vc = nav.visibleViewController {
            return topVC(of: vc)
        }
        if let vc = rootVC.presentedViewController {
            return topVC(of: vc)
        }
        return rootVC
    }
}
